<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;

class ViewGetSubCenter extends Common
{ 
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    { 
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;
  
        return $re;
    }


   /**
     * 获取csv数据
     * @param $post
     * @return array
     */
    public function getExportData($post)
    {
        $result   = [
            'status' => false,
            'data'   => [],
            'msg'    => '无可导出用户'
        ]; 
 
        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])
                    ->where($tableWhere['where'])
                    ->where($tableWhere['whereOr'])
                    ->order($tableWhere['order'])
                    ->select();
                    
        if (count($list) > 0) {
            $result['status'] = true;
            $result['msg']    = '导出成功';
            $result['data']   = $list;
            return $result;
        } else {
            //失败，导出失败
            return $result;
        }
    }


    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if (isset($post['dep_id']) && $post['dep_id'] != "") {
            $where[] = ['dep_id', 'eq', $post['dep_id']];
        }

        if (isset($post['sub_center_name']) && $post['sub_center_name'] != "") {
            $where[] = ['centerName', 'like', '%' . $post['sub_center_name'] . '%'];
        }

        if (isset($post['specialty_id']) && $post['specialty_id'] != "") {
            $where[] = ['specialty_id', 'eq', $post['specialty_id']];
        }
 
        $result['where'] = $where;
        $result['field'] = "*";
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }
}